package com.yunda.AJDeviceService.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunda.AJDeviceService.common.service.RedisService;
import com.yunda.AJDeviceService.dao.DeviceRunStatusDao;
import com.yunda.AJDeviceService.data.DeviceRunStatusData;
import com.yunda.AJDeviceService.data.DeviceRunTimeData;
import com.yunda.AJDeviceService.service.DeviceRunStatusService;
import com.yunda.AJDeviceService.service.DeviceRunTimeService;
import com.yunda.AJDeviceService.utils.DateUtils;

/**
 * AJDeviceService
 * @description 安检机运行状态
 * @author ljc
 * @create 2023年2月11日 下午1:59:15
 */
@Service
public class DeviceRunStatusServiceImpl extends ServiceImpl<DeviceRunStatusDao, DeviceRunStatusData> implements DeviceRunStatusService{


    @Value("${device.keepStatusTime}")
    private Integer keepStatusTime;

    @Autowired
    private RedisService redisService;
    @Autowired
    private DeviceRunTimeService deviceRunTimeService;
	/**
	 * 更新安检机运行状态
	 */
	@Override
	public boolean uploadDeviceStatus(DeviceRunStatusData statusData) {
		boolean keepStatus=!ObjectUtils.isEmpty(redisService.get(statusData.getDeviceCode()));
		redisService.set(statusData.getDeviceCode(), 1, keepStatusTime);
		if(keepStatus) {
			//写入数据库结束时间
			deviceRunTimeService.updateEndTime(statusData);
		}else {
			//写入数据库开始时间
			String nowTime=DateUtils.getTime();
			DeviceRunTimeData timeData=new DeviceRunTimeData();
			timeData.setFbCode(statusData.getFbCode());
			timeData.setDeviceSupplier(statusData.getDeviceSupplier());
			timeData.setDeviceCode(statusData.getDeviceCode());
			timeData.setStartTime(nowTime);
			timeData.setEndTime(nowTime);
			timeData.setCreateTime(nowTime);
			deviceRunTimeService.save(timeData);
		}
		return true;
	}
}
